package com.cuvretool.view.jframe;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;

import com.cuvretool.view.BaseFrame;
import com.cuvretool.view.BaseJPanel;
import com.cuvretool.view.draw.DisplayDraw;
import com.cuvretool.view.jpanel.JPanelCondition;
import com.cuvretool.view.jpanel.JPanelOperate;
import com.cuvretool.view.jpanel.JPanelWorkSpace;
/**
 * 主窗口
 * since 2015年1月13日 下午11:58:27
 * @author boy
 */
public class JFrameApplication extends BaseFrame {

	// ===========================================================
	// Constants
	// ===========================================================
	private static final long serialVersionUID = 1L;
	/**窗口面板*/
	protected BaseJPanel mContentPanel;
	// ===========================================================
	// Fields
	// ===========================================================
	/**菜单栏*/
	protected JMenuBar mMenuBar;
	/**绘制面板*/
	protected JPanelWorkSpace mWorkSpace;
	/**操作面板*/
	protected JPanelOperate mOperate;
	/**状态栏*/
	protected JPanelCondition mCondition;
	/**绘制区与操作区比列,区间(0,1)*/
	protected float mProportion;
	/**状态栏高度比例*/
	protected float mConditionHeigt = 0.03f;
	// ===========================================================
	// Constructors
	// ===========================================================

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================
	@Override
	public void init() {
		super.init();
		this.setTitle("CurveTool");
		this.setSize(mSize);
		this.setMinimumSize(mSize);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		File file = new File("res/icon.png");
		Image image = null;
		try {
			image = ImageIO.read(file);
		} catch (IOException e) {
			e.printStackTrace();
		}
		this.setIconImage(image);
		
		mContentPanel = new BaseJPanel();
		mContentPanel.setLayout(new BorderLayout());
		this.setContentPane(mContentPanel);
		mContentPanel.setVisible(true);
		
		mProportion = 0.3f;
		
		onCreateWorkMenuBar();
	}
	
	@Override
	public void run() {
		onCreateWorkPanel();
		
		mViewInputHandleListener.setOperatePanel(mOperate);
		
		this.addWindowListener(onCreateWindowAdapter());
		this.addComponentListener(onCreateComponentAdapter());
		
		DisplayDraw.obtain();
		DisplayDraw.obtain().run();
		
		this.repaint();
	}

	@Override
	public void resize() {
		mOperate.setPreferredSize(new Dimension((int) (mSize.width * mProportion), this.getHeight()));
		mWorkSpace.setPreferredSize(new Dimension((int) (mSize.width * (1-mProportion)), this.getHeight()));
		mCondition.setPreferredSize(new Dimension(this.getWidth(), (int) (mSize.height * mConditionHeigt)));
		mOperate.resize();
		mWorkSpace.resize();
		mCondition.resize();
		DisplayDraw.obtain().run();
	}
	// ===========================================================
	// Methods
	// ===========================================================
	/**
	 * 创建WindowAdapter
	 * @return
	 */
	private WindowAdapter onCreateWindowAdapter(){
		WindowAdapter listener = new WindowAdapter() {
			
			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
//				JOptionPane.showMessageDialog(JFrameApplication.this, "closing", "提示",JOptionPane.QUESTION_MESSAGE);
			}
			
		};
		return listener;
	}
	/**
	 * 创建ComponentAdapter
	 * @return
	 */
	private ComponentAdapter onCreateComponentAdapter(){
		ComponentAdapter componentListener = new ComponentAdapter(){

			@Override
			public void componentResized(ComponentEvent e) {
				resize();
			}

			@Override
			public void componentMoved(ComponentEvent e) {
				// TODO Auto-generated method stub
				
			}

		};
		return componentListener;
	}
	/**
	 * 创建Menu内容
	 */
	private void onCreateWorkMenuBar(){
		mMenuBar = new JMenuBar();
		JMenu file = new JMenu("文件");
		mMenuBar.add(file);
		JMenu set = new JMenu("设置");
		mMenuBar.add(set);
		JMenu help = new JMenu("帮助");
		mMenuBar.add(help);
		this.setJMenuBar(mMenuBar);
	}
	/**
	 * 创建Panel内容
	 */
	private void onCreateWorkPanel(){
		mOperate = new JPanelOperate();
		mOperate.setPreferredSize(new Dimension((int) (mSize.width * mProportion), this.getHeight()));
		mOperate.setBackground(Color.DARK_GRAY);
		mWorkSpace = new JPanelWorkSpace();
		mWorkSpace.setPreferredSize(new Dimension((int) (mSize.width * (1-mProportion)), this.getHeight()));
		mWorkSpace.setBackground(Color.gray);
		mCondition = new JPanelCondition();
		mCondition.setPreferredSize(new Dimension(this.getWidth(), (int) (mSize.height * mConditionHeigt)));
		mContentPanel.add(mOperate,BorderLayout.WEST);
		mContentPanel.add(mWorkSpace,BorderLayout.CENTER);
		mContentPanel.add(mCondition, BorderLayout.SOUTH);
		this.setVisible(true);
		mOperate.init();
		mWorkSpace.init();
		mCondition.init();
	}
	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
}
